package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiMessageService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;

@Controller
@RequestMapping(value = "/api/v1/message", method = { RequestMethod.POST })
public class ApiMessageController {
	private static Logger log = LoggerFactory.getLogger(ApiMerchantController.class);
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiMessageService apiMessageService;
	
	/**
	 * 获取消息列表
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage getMsgList(@RequestParam(value = "paramStr", required = true) String paramStr){
		ApiMessage msg=new ApiMessage();
		try {
			JSONObject dto = JSON.parseObject(paramStr);
		    msg = apiMessageService.getMsgList(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR, Constants.MSG_EXCEPTION);
			e.printStackTrace();
		} finally {
			return msg;
		}

		
	}
	
	/**
	 * 获取消息详情
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/detail", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage getMsgDetail(@RequestParam(value = "paramStr", required = true) String paramStr){
		ApiMessage msg=new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiMessageService.getMsgDetail(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR, Constants.MSG_EXCEPTION);
			e.printStackTrace();
		} finally {
			return msg;
		}

		
	}
    
}
